<?php
class CustomController extends AppController {

	public $name = 'custom';
	function beforeFilter() {
	    parent::beforeFilter(); 
		$this->C_auth->autoRedirect = false; //For override login
	    $this->C_auth->allowedActions = array(/* 'index','dellog','ulist','delcuter','uservip' */);

	}

	public function dellog() {	
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));
		$query = array(
				'fields' => array('shopcustomcat.*'),
				'conditions' => array('shopcustomcat.shop_id' => $this->my->id,'shopcustomcat.id' => $address_id),
				);	
	    $address_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:first]',$query);	

		if(empty($address_rs)) {
            $this->flash("该项不存在!", Router::url('custom/index/'));		
		}else {
			$query = array('keyid'=>$address_id);			
			POP::exe('[@@:UM shopcustomcat][@@:QU del]',$query);				
            $this->flash("执行成功！", Router::url('custom/index/'));		   
		}        		
	}

	public function index() {	
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));		
        if(!empty($this->data)) {
	        if(!empty($this->data['shopcustomcat']['rebate']) && !empty($this->data['shopcustomcat']['custom_cat_name']) ) {
                if($this->data['shopcustomcat']['id']) {		
				$address_id = $this->data['shopcustomcat']['id'];
			    }
				
				$query = array(
						'fields' => array('shopcustomcat.*'),
						'conditions' => array('shopcustomcat.custom_cat_name' => $this->data['shopcustomcat']['custom_cat_name'],'shopcustomcat.shop_id' => $this->my->id),
						);	
				$isarray = POP::exe('[@@:UM shopcustomcat][@@:QU find:first]',$query);				

			   
			    if(empty($isarray) || $address_id) {					
			        unset($this->data['shopcustomcat']['id']);
			        $this->data['shopcustomcat']['shop_id'] = $this->my->id;			
			        $safeData = $this->data['shopcustomcat'];
					$query  = array();				
					if(!empty($address_id)) {	
						$query['keyid'] = $address_id;				
						$query['set'] = $safeData;					
					}else {			
						$query = $safeData;		
					}
					POP::exe('[@@:UM shopcustomcat][@@:QU save]',$query);					
                    $this->flash("执行成功！", Router::url('custom/index/'));						
                }else {
                    $this->flash("该客户等级名称已经存在！", Router::url('custom/index/'));
                }
            } else {
                $this->flash("客户等级名称和折扣数不能为空！", Request::referer());
            }			
        }else {	

			$query = array(
					'fields' => array('shopcustomcat.*'),
					'conditions' => array('shopcustomcat.shop_id' => $this->my->id),
					);	
			$address_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:all]',$query);	
		
			$user_info=array(
				'name'=>'',
				'rebate'=>'',
			);
			if($address_id && $address_rs){
				foreach($address_rs as $value) {
					if($address_id == $value['shopcustomcat']['id']) {
						$user_info = $value['shopcustomcat'];
					}
				}
			}	
			$this->set('user_info',$user_info);		
			$this->set('address_rs',$address_rs);	
			$this->set('address_id',$address_id);
		}
	}	
	
	
	public function ulist() {
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));

		$query = array(
				'fields' => array('shopcustomcat.*'),
				'conditions' => array('shopcustomcat.shop_id' => $this->my->id),
				);	
		$cat_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:all]',$query);	
			
		$customcat = array();
	    foreach($cat_rs as $value) {
			$customcat[$value['shopcustomcat']['id']] = $value['shopcustomcat']['custom_cat_name'];
	    } 					
        if(!empty($this->data)) {
		    if(empty($this->data['shopcustomer']['customcat_id'])) {
			    $this->flash("该选择客户等级！", Request::referer());
			}else {	

				$query = array(
						'fields' => array('user.*'),
						'conditions' => array('user.username' => $this->data['shopcustomer']['user_name']),
						);	
				$userinfo = POP::exe('[@@:UM user][@@:QU find:first]',$query);	

			    if(empty($userinfo)) {
			        $this->flash("该客户用户名不存在！", Request::referer());	
			    }else {
					$query = array(
							'fields' => array('staff.*'),
							'conditions' => array('staff.staff_num' => $this->data['shopcustomer']['salesman_user_name'],'staff.shop_id' => $this->my->id),
							);	
					$isarray = POP::exe('[@@:UM staff][@@:QU find:first]',$query);
 	
			        if(!empty($isarray)) {                    					
						if($this->data['shopcustomer']['id']) {		
							$address_id = $this->data['shopcustomer']['id'];
						}
						$query = array(
								'fields' => array('shopcustomer.*'),
								'conditions' => array('shopcustomer.shop_id' => $this->my->id,'shopcustomer.user_name' => $this->data['shopcustomer']['user_name']),
								);	
						$isarray = POP::exe('[@@:UM shopcustomer][@@:QU find:first]',$query);					


						if(empty($isarray) || $address_id) {					
							unset($this->data['shopcustomer']['id']);
							$this->data['shopcustomer']['shop_id'] = $this->my->id;	
							$this->data['shopcustomer']['user_id'] = $userinfo['user']['id'];						
							$safeData = $this->data['shopcustomer'];
							
							$query  = array();				
							if(!empty($address_id)) {	
								$query['keyid'] = $address_id;				
								$query['set'] = $safeData;					
							}else {			
								$query = $safeData;		
							}
							POP::exe('[@@:UM shopcustomer][@@:QU save]',$query);							
							$this->flash("执行成功！", Router::url('custom/ulist/'));						
						}else {
							$this->flash("该客户名称已经存在！", Router::url('custom/ulist/'));
						}
					}else {
					    $this->flash("所属员工编号不存在！", Request::referer());
					}
                }
            }			
        }else {	
			$query = array(
					'fields' => array('shopcustomcat.*'),
					'conditions' => array('shopcustomcat.shop_id' => $this->my->id),
					);	
			$address_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:all]',$query);	
		
			$user_info=array(
				'user_name'=>'',
				'customcat_id'=>'0',
				'salesman_user_name'=>'',			
			);
			if($address_id && $address_rs){
				foreach($address_rs as $value) {
					if($address_id == $value['shopcustomer']['id']) {
						$user_info = $value['shopcustomer'];
					}
				}
			}	
			$this->set('customcat',$customcat);			
			$this->set('user_info',$user_info);		
			$this->set('address_rs',$address_rs);	
			$this->set('address_id',$address_id);
		}
	}	
	
	public function delcuter() {	
        $this->_teamRights();	
        $address_id=intval(Request::get_args('address_id'));	

		$query = array(
				'fields' => array('shopcustomcat.*'),
				'conditions' => array('shopcustomer.shop_id' => $this->my->id,'shopcustomer.id' => $address_id),
				);	
		$address_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:first]',$query);			
		
		
		if(empty($address_rs)) {
            $this->flash("该项不存在!", Router::url('custom/ulist/'));		
		}else {
			$query = array('keyid'=>$address_id);			
			POP::exe('[@@:UM shopcustomer][@@:QU del]',$query);			
            $this->flash("执行成功！", Router::url('custom/ulist/'));		   
		}        		
	}
	
	public function uservip() {	
		$query = array(
				'fields' => array('shopcustomer.*'),
				'conditions' => array('shopcustomer.user_name' => $this->my->username),
				);	
		$customer = POP::exe('[@@:UM shopcustomer][@@:QU find:all]',$query);		
		
        $shopid = array();
		foreach($customer as $value) {
		    $shopid[$value['shopcustomer']['shop_id']] = $value['shopcustomer']['shop_id'];
		}

		$query = array(
				'fields' => array('shopinfo.*'),
				'conditions' => array('shopinfo.id' => $shopid),
				);	
		$shop_info = POP::exe('[@@:UM shopinfo][@@:QU find:all]',$query);	
		
 		$shop = array();
	    foreach($shop_info as $value) {
			$shop[$value['shopinfo']['id']] = $value['shopinfo']['shop_name'];		
	    } 	
		$query = array(
				'fields' => array('shopcustomcat.*'),
				'conditions' => array('shopcustomcat.shop_id' => $shopid),
				);	
		$cat_rs = POP::exe('[@@:UM shopcustomcat][@@:QU find:all]',$query);	

 		$customcat = array();
	    foreach($cat_rs as $value) {
			$customcat[$value['shopcustomcat']['shop_id']][$value['shopcustomcat']['id']]['catname'] = $value['shopcustomcat']['custom_cat_name'];
			$customcat[$value['shopcustomcat']['shop_id']][$value['shopcustomcat']['id']]['rebate'] = $value['shopcustomcat']['rebate'];			
	    }  
        $this->set('customer',$customer);
        $this->set('customcat',$customcat);			
        $this->set('shop',$shop);	
	}		
	
}

?>